var db = require('../db/commonDB')

module.exports = {
    //注册
    addUser: function (user, cb) {
        var sql1 = 'select user_name,user_phone from user where user_name = ? or user_phone = ?'
        var params1 = [user.user_name, user.user_phone]
        db(sql1, params1, function (err, results) {
            if (err) {
                cb(err)
            } else if (results.length > 0) {
                cb(new Error('该用户已存在'))
            } else {
                var sql = 'insert into user(user_name,user_phone,user_pwd) values(?,?,?)'
                var params = [user.user_name, user.user_phone, user.user_pwd]
                db(sql, params, function (err, results) {
                    if (err) {
                        cb(err)
                    } else {
                        if (results.affectedRows > 0) {
                            cb(null, results) //注册成功，插入数据成功
                        } else {
                            cb(new Error('数据插入失败！'))
                        }
                    }
                })
            }
        })
    },
    //修改密码
    changePwd: function (userNewInfo, cb) {
        var sql = "update user set user_pwd=? where user_id=?";
        var params = [userNewInfo.newPwd, userNewInfo.userid];
        db(sql, params, function (err, results) {
          if (err) {
            cb(err);
          } else {
            if (results.affectedRows > 0) {
              cb(null,results);
            } else {
              cb(new Error("修改密码失败"));
            }
          }
        });
       
    },
    //确认账号密码登录
    checkName: function (user, cb) {
        var sqlQuery = `select user_pwd,user_id from user where user_name = ?`
        var paramsQuery = [user.user_name]
        db(sqlQuery, paramsQuery, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                cb(null, results)
            }
        })
    },
    searchUser:function(user,cb){
        var sqlQuery = `select * from user where user_name = ?`
        var paramsQuery = [user.user_name]
        db(sqlQuery,paramsQuery,function(err,results){
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('没有此用户信息！').message, null)
                }
            }
        })
    },

    //更改用户姓名、生日、性别的信息
    changeInfo: function (user, cb) {
        var sqlChange = `update user set user_name=?,user_sex=?,user_birth=?,user_height=?,user_weight=? where user_id=?`
        var paramsChange = [user.user_name, user.user_sex, user.user_birth,user.user_height,user.user_weight,user.user_id]
        db(sqlChange, paramsChange, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                if(results.affectedRows>0){
                    cb(null, results)
                }
                else{
                    cb(new Error)
                }
                
            }
        })
    },
    //获取用户信息
    getUserInfo: function (user, cb) {
        var sqlGet = `select * from user where user_id = ?`
        var paramsGet = user.user_id
        db(sqlGet, paramsGet, function (err, results) {
            if (err) {
                cb(err, null)
            } else {
                cb(null, results)
            }
        })
    },
    //查看浏览记录
    selectFootPrint: function (footprint, cb) {
        var sql = 'select * from footprint where user_id = ?;'
        var params = [footprint.user_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('没有此用户信息！').message, null)
                }
            }
        })
    },
    //删除浏览记录
    delFootPrint: function (footprint, cb) {
        var sql = 'delete from footprint where user_id = ?;'
        var params = [footprint.user_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, '删除成功！')
                } else {
                    cb(new Error('数据库删除失败！').message, null)
                }
            }
        })
    },
    //查看收藏记录
    viewCollection: function (favorites, cb) {
        var sql = 'select * from favorites where user_id = ?'
        var params = favorites.user_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看失败！').message, null)
                }
            }
        })
    },
    //根据用户id和商品id查看商品收藏状态
    getFavState: function (favorites, cb) {
        var sql = 'select * from favorites where user_id = ? and goods_id = ?'
        var params = [favorites.user_id,favorites.goods_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看失败！').message, null)
                }
            }
        })
    },
    //删除收藏记录
    delViewCollection: function (favorites, cb) {
        var sql = 'delete from favorites where user_id = ? and goods_id= ?'
        var params = [favorites.user_id, favorites.goods_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, '删除成功！')
                } else {
                    cb(new Error('删除失败！').message, null)
                }
            }
        })
    },
    //添加收藏商品
    addCollection: function (favorites, cb) {
        var sql = `insert into favorites(favorite_id,goods_id,user_id,goods_img,goods_title,goods_price,favorite_state) values(?,?,?,?,?,?,?)`
        var params = [favorites.favorite_id,  favorites.goods_id, favorites.user_id,favorites.goods_img,favorites.goods_title,favorites.goods_price,favorites.favorite_state]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('添加失败！').message, null)
                }
            }
        })
    },
    // 删除购物车商品
    delCartProduct:function(user,cb){
        var sql = 'delete from shopcart where shopcart_goodsID = ? and user_id = ? and shopcart_goods_size = ?'
        var params = [user.shopcart_goodsID,user.user_id,user.shopcart_goods_size]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除失败！').message, null)
                }
            }
        })
    },

    //删除已收藏商品
    delCollection: function (favorites, cb) {
        var sql = `delete from favorites where favorite_id = ?`
        var params = favorites.favorite_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除失败！').message, null)
                }
            }
        })
    },
    getAllCollection: function (favorites, cb) {
        var sql = `select * from favorites where favorite_id = ?`
        var params = favorites.favorite_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('显示失败！').message, null)
                }
            }
        })
    },
    //查看社区全部文章内容
    getCommunity:function (_,cb) {
        var sql = `select * from theme_design order by theme_id desc`
        var params = []
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('显示失败！').message, null)
                }
            }
        })
    },
    //获取社区部分
    getCommunityInfo:function(_,cb){
        var sql = `select theme_design.like_count,theme_design.theme_id,user.user_id,user.user_name,user.user_pic,theme_design.theme_title,theme_design.pic_url,theme_design.color
        from user join theme_design on user.user_id= theme_design.user_id order by theme_design.theme_id desc`
        var params = []
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('显示失败！').message, null)
                }
            }
        })
    },
    //根据文章id查询对应文章
    getEssayByThemeId:function(user,cb){
        var sql =`select * from theme_design where theme_id = ?`
        var params = user.theme_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('显示失败！').message, null)
                }
            }
        })
    },
    //社区点赞
    getColor:function (user,cb) {
        var sql = `update theme_design set user_id=?,like_count=? where theme_id=? `
        var params = [user.user_id,user.like_count,user.theme_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('显示失败！').message, null)
                }
            }
        })
    },
    //点赞添加
    addcolor: function (like_tb, cb) {
        var sql = `insert into like_tb(theme_id,user_id,color) values(?,?,?)`
        var params = [like_tb.theme_id,like_tb.user_id,like_tb.color]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('添加失败！').message, null)
                }
            }
        })
    },
    //取消点赞删除
    delcolor: function (like_tb, cb) {
        var sql = `delete from like_tb where theme_id=? and user_id=?`
        var params = [like_tb.theme_id,like_tb.user_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除失败！').message, null)
                }
            }
        })
    },
    viewcolor: function (like_tb, cb) {
        var sql = 'select * from like_tb where user_id =?'
        var params = like_tb.user_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看失败！').message, null)
                }
            }
        })
    },

    //添加评论
    addconm: function (p_comm, cb) {
        var sql = `insert into p_comm(user_id,goods_id,product_detail,product_data,color,like_count,user_name,user_pic) values(?,?,?,?,?,?,?,?)`
        var userId=parseInt(p_comm.user_id)
        var params = [userId,p_comm.goods_id,p_comm.product_detail,p_comm.product_data,"black",0,p_comm.user_name,p_comm.user_pic]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('添加失败！').message, null)
                }
            }
        })
    },
    //查看评论
    viewconm: function (p_comm, cb) {
        var sql = 'select * from p_comm where goods_id =?'
        var goodsId=parseInt(p_comm.goods_id)
        var params = goodsId
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看失败！').message, null)
                }
            }
        })
    },
    getcomnColor:function (user,cb) {
        var sql = `update p_comm set like_count=? where product_comm_id=? `
        var params = [user.like_count,user.product_comm_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('显示失败！').message, null)
                }
            }
        })
    },
    //根据用户id查看点赞表
    viewuserconm: function (com_like, cb) {
        var sql = 'select * from com_like where user_id =?'
        var params = com_like.user_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看失败！').message, null)
                }
            }
        })
    },
      //评论点赞添加
      comaddcolor: function (com_like, cb) {
        var sql = `insert into com_like(product_comm_id,user_id,color) values(?,?,?)`
        var params = [com_like.product_comm_id,com_like.user_id,com_like.color]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('添加失败！').message, null)
                }
            }
        })
    },
    //删除点赞
    delcomcolor: function (com_like, cb) {
        var sql = `delete from com_like where product_comm_id=? and user_id=?`
        var params = [com_like.product_comm_id,com_like.user_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err.message, null)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除失败！').message, null)
                }
            }
        })
    },
    //通过用户id获取用户对应购物车全部数据
    getAllProuductById:function(user,cb){
        var sql = 'select * from shopcart where user_id = ?'
        var params = user.user_id
        db(sql,params,function(err,results){
            if(err){
                cb(err.message,null)
            }else{
                if(results.length > 0 ){
                    cb(null,results)
                }else{
                    cb(new Error('查看失败!'.message,null))
                }
            }
        })
    },
    //通过文章的文章id获取用户信息
    getUserInfoBytheme:function(user,cb){
        var sql = `select user.user_name,user.user_pic from user join theme_design on user.user_id= theme_design.user_id where theme_design.theme_id = ?`
        var params = user.theme_id
        db(sql,params,function(err,results){
            if(err){
                cb(err.message,null)
            }else{
                if(results.length > 0 ){
                    cb(null,results)
                }else{
                    cb(new Error('查看失败!',null))
                }
            }
        })
    },
    //发布社区文章
    releaseArticle:function(theme_design,cb){
        var sql = `insert into theme_design(user_id,theme_title,pic_url,theme_content,color,forward_count,like_count) values(?,?,?,?,'black',0,0)`
        var params = [theme_design.user_id,theme_design.theme_title,theme_design.pic_url,theme_design.theme_content,theme_design.color,theme_design.forward_count,theme_design.like_count]
        db(sql,params,function(err,results){
            if(err){
                cb(err.message,null)
            }else{
                if(results.affectedRows > 0 ){
                    cb(null,results)
                }else{
                    cb(new Error('发布失败!'.message,null))
                }
            }
        })
    },
}